//
//  GMFDrawingVC.m
//  SplunkMobile
//
//  Created by lijunying on 1/19/2016.
//  Copyright © 2016 Gome. All rights reserved.
//

typedef enum
{
    kCLAuthorizationStatusNotDetermined = 0, // 用户尚未做出选择这个应用程序的问候
    kCLAuthorizationStatusRestricted,        // 此应用程序没有被授权访问的照片数据。可能是家长控制权限
    kCLAuthorizationStatusDenied,            // 用户已经明确否认了这一照片数据的应用程序访问
    kCLAuthorizationStatusAuthorized         // 用户已经授权应用访问照片数据
}
CLAuthorizationStatus;


typedef enum
{
    kLine = 0,
    kCurve,    // 1
    kRetangle, // 2
    kEllipse,  // 3
    kArrow,    // 4
    kText      // 5
}
GMKSelectionStatus;

#import "GMFColorButton.h"
#import "GMFTextView.h"
#import "TextImageView.h"
#import "GMFDrawingVC.h"
#import "ACEDrawingView.h"
#import "GMFUpperToolbarView.h"
#import "GMFBottomToolbarView.h"
#import "GMFShapeView.h"
#import "GMFColorView.h"
//#import "IQLabelView.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "GMToast.h"
#import "SplunkMobile-Swift.h"

#pragma mark shareObject
#import "weChatImageObject.h"
#import "weChatEngine.h"
#import "weChatResult.h"

#import "QQImageObject.h"
#import "QQEngine.h"
#import "QQResult.h"

@interface GMFDrawingVC ()
<
UIActionSheetDelegate,
UIAlertViewDelegate,
UIScrollViewDelegate,
UpperToolbarViewDelegate,
BottomToolbarViewDelegate,
ColorViewDelegate,
ShapeViewDelegate,
//IQLabelViewDelegate,
ACEDrawingViewDelegate,
ACEDelegate,
ACETouchLocationDelegate,
ACESelectedDelegate,
ACEDoubleTagsDelegate,
ACEReselectTextDelegate,
ACEHideBottomDelegate
>

{
//    IQLabelView *currentlyEditingLabel;
//    NSMutableArray *labels;
}

@property (nonatomic, strong) UIImage *imgEditedScreenshot;
@property (nonatomic, weak) UIScrollView *scrvZoom;
@property (nonatomic, weak) ACEDrawingView *viewPad;
@property (nonatomic, weak) GMFUpperToolbarView *viewUpperToolbar;
@property (nonatomic, weak) GMFBottomToolbarView *viewBottomToolbar;
@property (nonatomic, weak) GMFColorView *viewColor;
@property (nonatomic, weak) GMFShapeView *viewShape;
@property (nonatomic, weak) UIImageView *imgvBackground;
@property (nonatomic, assign) BOOL isDrawingMode;
@property (nonatomic, strong) UIColor *colorDrawingAndText;
//@property (nonatomic, strong) IQLabelView *currentlyEditingLabel;
@property (nonatomic, strong) NSMutableArray *maryLabels;
@property (nonatomic, assign) NSInteger intShapeMark;
@property (nonatomic, assign) BOOL isTextButtonClicked;
@property (nonatomic, assign) BOOL isSubToolBarShow;
@property (nonatomic, assign) BOOL isRecur;

//@property (nonatomic, assign) NSInteger intSelectedButton;

@property (nonatomic, assign) BOOL isArrowShow;

@property (nonatomic, assign) BOOL isSecondAndLaterText;

//@property (weak, nonatomic) TextImageView *textImage;

@property (nonatomic, assign) GMKSelectionStatus kSelectionStatus;

@property (nonatomic, strong) UIColor *clrCurrentDrawingAndText;

@property (nonatomic, assign) GMKSelectionStatus kPreviousShape;


@end

@implementation GMFDrawingVC

#pragma mark - *********************************** View Lifecycle **********************************

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self initVariable]; /// 初始化变量
    [self initViews]; /// 初始化界面
}

- (void)dealloc
{

}


#pragma mark - *********************************** Init All Views **********************************

- (void)initVariable
{
    _isDrawingMode = YES;
}

- (void)initViews
{
    /// 根据界面情况，决定是单独写函数，或直接写Views
    [self initMainViews];
}

- (void)initMainViews
{
    UIScrollView *scrvZoom = [[ UIScrollView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:scrvZoom];
    scrvZoom.canCancelContentTouches = YES;
    scrvZoom.delaysContentTouches = NO;
    
    scrvZoom.panGestureRecognizer.minimumNumberOfTouches = 2;
    
//    scrvZoom.panGestureRecognizer.maximumNumberOfTouches = 2;
    
    self.scrvZoom = scrvZoom;
    
    /// 创建背景图
    UIImageView *imgvBackground = [[UIImageView alloc] init];
    imgvBackground.image = _imgScreenshot;
    [imgvBackground setFrame:CGRectMake(0 + 10, 0 + 10, _imgScreenshot.size.width - 20, _imgScreenshot.size.height - 20)];
    imgvBackground.userInteractionEnabled = YES;
    [self.scrvZoom addSubview:imgvBackground];
    self.imgvBackground = imgvBackground;
    
    _scrvZoom.contentSize = _imgScreenshot.size;
    _scrvZoom.delegate = self;
    _scrvZoom.maximumZoomScale = 2.0;
    _scrvZoom.minimumZoomScale = 1.0;

    _clrCurrentDrawingAndText = kColorDefaultBlue;
    _kPreviousShape = 1;
    
    /// 创建画图板
    ACEDrawingView *viewPad = [[ACEDrawingView alloc] initWithFrame:_imgvBackground.bounds];
    viewPad.backgroundColor = [UIColor clearColor];
    viewPad.multipleTouchEnabled = YES;
    /// 设置线颜色
    viewPad.lineColor = kColorDefaultBlue;
    /// 默认矩形工具
    viewPad.drawTool = ACEDrawingToolTypePen;
    /// 设置线宽
    viewPad.lineWidth = 3;
    
    viewPad.canDraw = YES;
    /// 是否笔模式
//    viewPad.isPen = YES;
    viewPad.delegate = self;
    viewPad.aDelegate = self;
    viewPad.lDelegate = self;
    viewPad.sDelegate = self;
    viewPad.tDelegate = self;
    viewPad.rDelegate = self;
    viewPad.hDelegate = self;
    [_imgvBackground addSubview:viewPad];
    self.viewPad = viewPad;
    
    _kSelectionStatus = 1;
    
    /// 垃圾桶按钮
    UIButton *btnTrash = [[UIButton alloc] initWithFrame:CGRectMake(screenW - fltButtonWidth - 15, 80, fltButtonWidth + 15, fltButtonWidth + 15)];
//    btnTrash.backgroundColor = [UIColor redColor];
    [btnTrash setImage:[UIImage imageNamed:@"btn_trash-z"] forState:UIControlStateNormal];
    [btnTrash addTarget:self action:@selector(trashButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    btnTrash.hidden = YES;
    [self.view addSubview:btnTrash];
    self.btnTrash = btnTrash;
    
    /// 创建画图工具栏
//    GMFToolbarView *viewToolbar = [[GMFToolbarView alloc] initWithFrame:CGRectMake(0, 20 - fltButtonWidth, screenW, fltButtonWidth)];
//    viewToolbar.delegate = self;
//    [self.view addSubview:viewToolbar];
//    self.viewToolbar = viewToolbar;
    
    /// 顶部工具栏
    GMFUpperToolbarView *viewUpperToolbar = [[GMFUpperToolbarView alloc] initWithFrame:CGRectMake(0, 20 - fltButtonWidth, screenW, fltButtonWidth - 8)];
    viewUpperToolbar.delegate = self;
    [self.view addSubview:viewUpperToolbar];
    self.viewUpperToolbar = viewUpperToolbar;
    
    /// 创建颜色板
    GMFColorView *viewColor = [[GMFColorView alloc] initWithFrame:CGRectMake(0, screenH, screenW, 50)];
    viewColor.delegate = self;
    [self.view addSubview:viewColor];
    self.viewColor = viewColor;
    
    /// 创建形状版
    GMFShapeView *viewShape = [[GMFShapeView alloc] initWithFrame:CGRectMake(0, screenH, screenW, 50)];
    viewShape.delegate = self;
    [self.view addSubview:viewShape];
    self.viewShape = viewShape;
    
    _intShapeMark = 1;
    
    /// 底部工具栏
    GMFBottomToolbarView *viewBottomToolbar = [[GMFBottomToolbarView alloc] initWithFrame:CGRectMake(0, screenH, screenW, 44)];
    viewBottomToolbar.delegate = self;
    [self.view addSubview:viewBottomToolbar];
    self.viewBottomToolbar = viewBottomToolbar;
    
    UIView *viewWhite = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenW, 20)];
    viewWhite.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:viewWhite];
    
    /// 工具栏出现动画
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.5 animations:^{
            [_viewUpperToolbar  setFrame:CGRectMake(0, 20, screenW, fltButtonWidth - 8)];
            [_viewBottomToolbar setFrame:CGRectMake(0, screenH - 44, screenW, 44)];
            [_viewShape         setFrame:CGRectMake(0, screenH - fltButtonWidth, screenW, fltButtonWidth + fltAddHeight)];
            [_viewColor         setFrame:CGRectMake(0, screenH - fltButtonWidth, screenW, fltButtonWidth)];
        } completion:^(BOOL finished) {
            
        }];
    });
}


#pragma mark - ************************************* Functions *************************************

- (void)normalZoomScale
{
//    _viewPad.userInteractionEnabled = YES;
//    _viewPad.multipleTouchEnabled = YES;
    _scrvZoom.zoomScale = 1.0;
}


#pragma mark 图形工具栏/颜色工具栏的动画

- (void)movingView:(UIView *)view
{
    NSTimeInterval duration = 0.3;
    
    /// 如果是图形view
    if ([view isKindOfClass:[GMFShapeView class]])
    {
        if (_viewShape.isShapeViewShow)
        {
            [UIView animateWithDuration:duration animations:^{
                [_viewShape setFrame:CGRectMake(0, screenH - 44, screenW, 50)];
                _viewShape.alpha = 0;
            } completion:^(BOOL finished) {
                _viewShape.isShapeViewShow = NO;
            }];
        }
        else
        {
            if (_viewColor.isColorViewShow)
            {
                [UIView animateWithDuration:duration animations:^{
                    [_viewColor setFrame:CGRectMake(0, screenH - 44, screenW, 50)];
                    _viewColor.alpha = 0;
                } completion:^(BOOL finished) {
                    _viewColor.isColorViewShow = NO;
                    
                    [UIView animateWithDuration:duration animations:^{
                        [_viewShape setFrame:CGRectMake(0, screenH - 94, screenW, 50)];
                        _viewShape.alpha = kToolBarAlpha;
                    } completion:^(BOOL finished) {
                        _viewShape.isShapeViewShow = YES;
                    }];
                }];
            }
            else
            {
                [UIView animateWithDuration:duration animations:^{
                    [_viewShape setFrame:CGRectMake(0, screenH - 94, screenW, 50)];
                    _viewShape.alpha = kToolBarAlpha;
                } completion:^(BOOL finished) {
                    _viewShape.isShapeViewShow = YES;
                }];
            }
        }
        
        _isSubToolBarShow = !_isSubToolBarShow;
    }
    /// 如果是颜色view
    else if ([view isKindOfClass:[GMFColorView class]])
    {
        if (_viewColor.isColorViewShow)
        {
            [UIView animateWithDuration:duration animations:^{
                [_viewColor setFrame:CGRectMake(0, screenH - 44, screenW, 50)];
                _viewColor.alpha = 0;
            } completion:^(BOOL finished) {
                _viewColor.isColorViewShow = NO;
            }];
        }
        else
        {
            if (_viewShape.isShapeViewShow)
            {
                [UIView animateWithDuration:duration animations:^{
                    [_viewShape setFrame:CGRectMake(0, screenH - 44, screenW, 50)];
                    _viewShape.alpha = 0;
                } completion:^(BOOL finished) {
                    _viewShape.isShapeViewShow = NO;
                    
                    [UIView animateWithDuration:duration animations:^{
                        [_viewColor setFrame:CGRectMake(0, screenH - 94, screenW, 50)];
                        _viewColor.alpha = kToolBarAlpha;
                    } completion:^(BOOL finished) {
                        _viewColor.isColorViewShow = YES;
                    }];
                }];
            }
            else
            {
                [UIView animateWithDuration:duration animations:^{
                    [_viewColor setFrame:CGRectMake(0, screenH - 94, screenW, 50)];
                    _viewColor.alpha = kToolBarAlpha;
                } completion:^(BOOL finished) {
                    _viewColor.isColorViewShow = YES;
                }];
            }
        }
    }
}


#pragma mark 隐藏二级菜单

- (void)hideSubToolbar
{
    if (_viewColor.frame.origin.y == screenH - 94)
    {
        [self movingView:_viewColor];
    }
    
    if (_viewShape.frame.origin.y == screenH - 94)
    {
        [self movingView:_viewShape];
    }
}


#pragma mark 缩放所控制的view

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
//    if (_isDrawingMode)
//    {
//        return nil;
//    }
    
    return _imgvBackground;
}


#pragma mark 截图

- (void)screenshotForDrawingPad:(BOOL)isSavedImageToAlbum
{
    UIGraphicsBeginImageContextWithOptions(_imgvBackground.bounds.size, NO, 0.0);
    [_imgvBackground.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    if (isSavedImageToAlbum)
    {
        UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);
        
        ALAuthorizationStatus author = [ALAssetsLibrary authorizationStatus];
        
        if (author == kCLAuthorizationStatusRestricted || author == kCLAuthorizationStatusDenied)
        {
            UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@""
                                                              message:@"请在”设置-隐私-照片“选项中允许访问你的照片"
                                                             delegate:nil
                                                    cancelButtonTitle:@"OK"
                                                    otherButtonTitles:nil, nil];
            [alertView show];
        }
        else
        {
            [self showToastWithView:self.view text:@"图片已保存至相册"];
//            UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:@""
//                                                              message:@"截图已保存到相册"
//                                                             delegate:nil
//                                                    cancelButtonTitle:@"OK"
//                                                    otherButtonTitles:nil, nil];
//            [alertView show];
        }
    }
    else
    {
        _imgEditedScreenshot = [[UIImage alloc] initWithCGImage:image.CGImage];
    }
}

- (void)previousSelectionStatus:(GMKSelectionStatus)status
{
    switch (status)
    {
        case 0:
            [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_line2"] forState:UIControlStateNormal];
//            _viewPad.drawTool = ACEDrawingToolTypeLine;
            _intShapeMark = 0;
            [_viewBottomToolbar changeRedViewPosition:2];
            
            break;
        case 1:
            [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_curve2"] forState:UIControlStateNormal];
//            _viewPad.drawTool = ACEDrawingToolTypePen;
            _intShapeMark = 1;
            [_viewBottomToolbar changeRedViewPosition:2];
            break;
        case 2:
            [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_rectangle2"] forState:UIControlStateNormal];
//            _viewPad.drawTool = ACEDrawingToolTypeRectagleStroke;
            _intShapeMark = 2;
            [_viewBottomToolbar changeRedViewPosition:2];
            break;
        case 3:
            [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_ellipse2"] forState:UIControlStateNormal];
//            _viewPad.drawTool = ACEDrawingToolTypeEllipseStroke;
            _intShapeMark = 3;
            break;
        case 4:
            [_viewBottomToolbar.btnArrow setImage:[UIImage imageNamed:@"btn_arrow2"] forState:UIControlStateNormal];
//            _viewPad.drawTool = ACEDrawingToolTypeArrow;
            _intShapeMark = 4;
            [_viewBottomToolbar changeRedViewPosition:1];
            break;
            
        default:
            break;
    }
}


#pragma mark - ********************************* Button Touch Event ********************************

#pragma mark 选中状态

- (void)trashButtonClick:(UIButton *)button
{
    NSLog(@"点中了");
    [_viewPad deleteTheSelectedDrawingTool];
    
    _btnTrash.hidden = YES;
}


#pragma mark shareButtonClick
- (void)showToastWithView:(UIView *)view text:(NSString *)str
{
    GMToast *toast =[[GMToast alloc] initWithView:view text:str duration:1.5];
    [toast show];
}

-(void)weiXinShareClick
{
    NSData *imageData1 = UIImagePNGRepresentation(_imgEditedScreenshot);
    UIImage *tmpImage = [UIImage imageWithData:imageData1];
    
    NSData *imageData = UIImageJPEGRepresentation(tmpImage, 1);
    
    weChatImageObject *imageObject =[weChatImageObject createWeChatVideoObjectWithScece:WeChatSceneSession
                                                                                  title:@"国美Plus" // self.shareTitle
                                                                                   desc:@"请注意图片标注内容"
                                                                              thumbData:imageData
                                                                               imageUrl:@"www.gomeplus.com"];
    // https://www.gomeplus.com/ www.gome.com.cn
    weChatEngine *wxEngin =[weChatEngine shareWeChatEngine];
    [wxEngin sendReqWithObject:imageObject callBackBlock:^(weChatResult *result) {
        
        NSLog(@"weixin info =%@",[result resultErrorInfo]);
        [self showToastWithView:self.view text:[result resultErrorInfo]];
    }];
}

- (void)QQShareClick
{
    NSData *imageData = UIImagePNGRepresentation(_imgEditedScreenshot);
    
    QQImageObject *imageObject =[QQImageObject createQQVideoWithQQScene:QQSceneSession
                                                                  title:self.shareTitle
                                                                   desc:@"请注意图片标注内容" imageData:imageData
                                                       preViewImageData:imageData];
    
    QQEngine *qqEngine =[QQEngine shareQQSDKEngine];
    [qqEngine sendReqWithObject:imageObject block:^(QQResult *result) {
        if(result)
        {
            NSLog(@"QQ info =%@",[result errorInfo]);
            [self showToastWithView:self.view text:[result errorInfo]];
        }
    }];
}


#pragma mark - *********************************** Delegate : System **********************************

#pragma mark UIAlertViewDelegate

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag == 9000000)
    {
        if (buttonIndex == 0)
        {
            
        }
        else if (buttonIndex == 1)
        {
            [UIView animateWithDuration:0.6 animations:^{
                [_viewUpperToolbar  setFrame:CGRectMake(0, 20 - fltButtonWidth, screenW, fltButtonWidth)];
                [_viewBottomToolbar setFrame:CGRectMake(0, screenH, screenW, fltButtonWidth)];
                [_viewShape   setFrame:CGRectMake(0, screenH, screenW, fltButtonWidth)];
                [_viewColor   setFrame:CGRectMake(0, screenH, screenW, fltButtonWidth)];
            } completion:^(BOOL finished) {
                [self dismissViewControllerAnimated:NO completion:^{
                    
                }];
            }];
        }
        
        
    }
    else if (alertView.tag == 9000001)
    {
        if (buttonIndex == 0)
        {
            
        }
        else if (buttonIndex == 1)
        {
            [_viewPad clear];
            
            for (GMFTextView *viewSub in _viewPad.subviews)
            {
                [viewSub removeFromSuperview];
            }
            
            [_viewUpperToolbar.btnUndo setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
            [_viewUpperToolbar.btnRedo setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
    }
    
}


#pragma mark UIActionSheetDelegate

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex)
    {
        case 0:
            [self weiXinShareClick];
            break;
        case 1:
            [self QQShareClick];
            break;
        case 2:
            [self screenshotForDrawingPad:YES];
            break;
            
        default:
            break;
    }
}


#pragma mark - *********************************** Delegate : Custom **********************************

- (void)drawingViewHideBottom:(ACEDrawingView *)view isHidden:(BOOL)isHidden
{
    if (isHidden)
    {
        [_viewBottomToolbar setHidden:YES];
        [_viewColor setHidden:YES];
        [_viewShape setHidden:YES];
    }
    else
    {
        [_viewBottomToolbar setHidden:NO];
        [_viewColor setHidden:NO];
        [_viewShape setHidden:NO];
    }
}


- (void)drawingView:(ACEDrawingView *)view didEndDrawUsingTool:(id<ACEDrawingTool>)tool
{
    [_viewUpperToolbar.btnUndo setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
}

- (void)drawingViewReselect:(ACEDrawingView *)view
{
    _btnTrash.hidden = YES;
    
    
//    _isTextButtonClicked = YES;
//    
////    _viewPad.canDraw = NO;
////    
    [self hideSubToolbar];
//
//    [_viewBottomToolbar.btnAddText setImage:[UIImage imageNamed:@"btn_text2"] forState:UIControlStateNormal];
//    
//    [_viewBottomToolbar changeRedViewPosition:3];
}

- (void)drawingViewDoubleTags:(ACEDrawingView *)view
{
    [self normalZoomScale];
}

- (void)drawingView:(ACEDrawingView *)view isSelected:(BOOL)selected
{
    if (selected)
    {
        _btnTrash.hidden = NO;
    }
    else
    {
        _btnTrash.hidden = YES;
    }
}

- (void)drawingView:(ACEDrawingView *)view locationX:(float)x locationY:(float)y
{
//    [self hideSubToolbar];
    
    if (_isTextButtonClicked)
    {
        // 避免选中状态下点击空白处创建新文本框
        if (_viewPad.isRed)
        {
//            NSLog(@"red");
            _viewPad.isRed = NO;
  
            return;
        }
        
        // 循环切换两个状态：点击空白处创建新文本框/点击空白处取消编辑
//        if (!_isRecur)
//        {
            _viewPad.canDraw = NO;
        
        
            if (y > screenH - 216 - 80)
            {
                _viewPad.shouldViewMoveUp = YES;
                
            }
            else
            {
                _viewPad.shouldViewMoveUp = NO;
            }
            
            [_viewPad addTextWithX:x andY:y];
//        }
        
//        _isRecur = !_isRecur;
        
    }
    else
    {
        
    }
    
}


- (void)drawingView:(ACEDrawingView *)view isOneTouch:(BOOL)oneTouch
{
    [self hideSubToolbar];
    
    if (oneTouch)
    {
        _isDrawingMode = YES;
        
        _scrvZoom.multipleTouchEnabled = NO;
        _scrvZoom.scrollEnabled = NO;
//        _viewPad.userInteractionEnabled = YES;
    }
    else
    {
        _isDrawingMode = NO;
        
        _scrvZoom.multipleTouchEnabled = YES;
        _scrvZoom.scrollEnabled = YES;
//        _viewPad.userInteractionEnabled = NO;
        
        
    }
    
}

- (void)upperToolbarView:(GMFUpperToolbarView *)toolbarView buttonTagString:(NSString *)string
{
    /// 关闭
    if ([string isEqualToString:@"1000"])
    {
        if (_viewPad.isKeyboardShow)
        {
            //            return;
            [_viewPad tapSelf];
        }
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                            message:@"退出将不保留您的批注文件，确认退出？"
                                                           delegate:nil
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"确认", nil];
        
        alertView.tag = 9000000;
        alertView.delegate = self;
        
        [alertView show];
    }
    
    /// 缩放
    else if ([string isEqualToString:@"1001"])
    {
        /// 如果当前是绘图模式
        if (_isDrawingMode)
        {
            [self showToastWithView:self.view text:@"缩放模式：通过双指缩放"];
            
            _scrvZoom.multipleTouchEnabled = YES;
            _scrvZoom.scrollEnabled = YES;
            
            _viewPad.userInteractionEnabled = NO;
            
            _viewUpperToolbar.btnZoom.backgroundColor = [UIColor colorWithRed:176/255.0 green:223/255.0 blue:229/255.0 alpha:1.0];
//            _viewUpperToolbar.btnDraw.backgroundColor = [UIColor grayColor];
//            _viewUpperToolbar.btnPalette.backgroundColor = [UIColor grayColor];
            _viewUpperToolbar.btnUndo.backgroundColor = [UIColor grayColor];
            _viewUpperToolbar.btnRedo.backgroundColor = [UIColor grayColor];
            _viewUpperToolbar.btnClear.backgroundColor = [UIColor grayColor];
//            _viewUpperToolbar.btnAddText.backgroundColor = [UIColor grayColor];
            
            _isDrawingMode = NO;
            
            if (_viewShape.isShapeViewShow)
            {
                [self movingView:_viewShape];
            }
            
            if (_viewColor.isColorViewShow)
            {
                [self movingView:_viewColor];
            }
        }
        else
        {
            [self showToastWithView:self.view text:@"批注模式：画图和编辑文字"];
            
            _scrvZoom.multipleTouchEnabled = NO;
            _scrvZoom.scrollEnabled = NO;
            _viewPad.userInteractionEnabled = YES;
            
            _viewUpperToolbar.btnZoom.backgroundColor = [UIColor clearColor];
//            _viewUpperToolbar.btnDraw.backgroundColor = [UIColor clearColor];
//            _viewUpperToolbar.btnPalette.backgroundColor = [UIColor clearColor];
            _viewUpperToolbar.btnUndo.backgroundColor = [UIColor clearColor];
            _viewUpperToolbar.btnRedo.backgroundColor = [UIColor clearColor];
            _viewUpperToolbar.btnClear.backgroundColor = [UIColor clearColor];
//            _viewUpperToolbar.btnAddText.backgroundColor = [UIColor clearColor];
            
            _isDrawingMode = YES;
        }
    }
    /// 画图
    else if ([string isEqualToString:@"1002"])
    {
        if (_isDrawingMode)
        {
            [self movingView:_viewShape];
        }
    }
    /// 调色板
    else if ([string isEqualToString:@"1003"])
    {
        if (_isDrawingMode)
        {
            [self movingView:_viewColor];
        }
    }
    /// 后退
    else if ([string isEqualToString:@"1004"])
    {
//        if (_viewPad.isKeyboardShow)
//        {
//            //            return;
            [_viewPad tapSelf];
//        }
//        if (_isDrawingMode)
//        {
            [_viewPad undoLatestStep];
//        }
        
        if (![_viewPad canUndo])
        {
            NSLog(@"不能后退了");
            
            [_viewUpperToolbar.btnUndo setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
        else
        {
            [_viewUpperToolbar.btnUndo setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
        
        if (![_viewPad canRedo])
        {
            [_viewUpperToolbar.btnRedo setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
        else
        {
            [_viewUpperToolbar.btnRedo setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    /// 前进
    else if ([string isEqualToString:@"1005"])
    {
//        if (_viewPad.isKeyboardShow)
//        {
//            //            return;
            [_viewPad tapSelf];
//        }
        
//        if (_isDrawingMode)
//        {
            [_viewPad redoLatestStep];
//        }
        if (![_viewPad canRedo])
        {
            NSLog(@"不能前进了");
            
            [_viewUpperToolbar.btnRedo setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
        
        if (![_viewPad canUndo])
        {
            NSLog(@"不能后退了");
            
            [_viewUpperToolbar.btnUndo setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        }
        else
        {
            [_viewUpperToolbar.btnUndo setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }
    }
    /// 清除
    else if ([string isEqualToString:@"1006"])
    {
        if (_viewPad.isKeyboardShow)
        {
            //            return;
            [_viewPad tapSelf];            
        }
        
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@""
                                                            message:@"清空将不保留您的批注记录，是否清空？"
                                                           delegate:nil
                                                  cancelButtonTitle:@"取消"
                                                  otherButtonTitles:@"清空", nil];
        alertView.tag = 9000001;
        
        alertView.delegate = self;
        
        [alertView show];

    }

    /// 发送
    else if ([string isEqualToString:@"1008"])
    {
        if (_viewPad.isKeyboardShow)
        {
//            return;
            [_viewPad tapSelf];
//            [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
            
        }
        
        [self hideSubToolbar];
        
        [self screenshotForDrawingPad:NO];

        //截屏成功
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"分享与保存"
                                                                 delegate:self
                                                        cancelButtonTitle:@"取消"
                                                   destructiveButtonTitle:nil
                                                        otherButtonTitles:@"微信",@"QQ",@"保存到相册", nil];
        [actionSheet showInView:self.view];
    }
}


- (void)bottomToolbarView:(GMFBottomToolbarView *)toolbarView buttonTagString:(NSString *)string
{
    /// 调色板
    if ([string isEqualToString:@"2000"])
    {
//        if (_isDrawingMode)
//        {
            [self movingView:_viewColor];
        
        [_viewPad changeColor:_clrCurrentDrawingAndText];
//        _viewPad.textNormalColor = _clrCurrentDrawingAndText;
//        _viewPad.textSelectColor = _clrCurrentDrawingAndText;
//        }
        _viewPad.textNormalColor = _clrCurrentDrawingAndText;
        
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"changeColor" object:nil];
        
        if ((_viewPad.currentText != nil) && (CGColorEqualToColor(_viewPad.currentText.layer.borderColor, kColorPointBlue.CGColor)))
        {
//            [[NSNotificationCenter defaultCenter] postNotificationName:@"changeColor" object:_viewPad];
            _viewPad.currentText.textColor = _clrCurrentDrawingAndText;
        }
    }
    /// 画箭头
    else if ([string isEqualToString:@"2001"])
    {
//        if (_isDrawingMode)
//        {
            _kSelectionStatus = 4;
            _viewPad.canDraw = YES;
            _viewPad.drawTool = ACEDrawingToolTypeArrow;
        
        _viewPad.txtmode = 0;
        
            _isTextButtonClicked = NO;
            
            _isArrowShow = YES;
            
            [self hideSubToolbar];
            
            [_viewBottomToolbar.btnAddText setImage:[UIImage imageNamed:@"btn_text"] forState:UIControlStateNormal];
            [_viewBottomToolbar.btnAddText setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
            [_viewBottomToolbar.btnArrow setImage:[UIImage imageNamed:@"btn_arrow2"] forState:UIControlStateNormal];
            [_viewBottomToolbar.btnArrow setTitleColor:kColorDefaultRed forState:UIControlStateNormal];
            [_viewBottomToolbar changeRedViewPosition:1];
        
            [_viewBottomToolbar.btnDraw setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
            if (_intShapeMark == 0)
            {
                [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_line_down"] forState:UIControlStateNormal];
            }
            else if (_intShapeMark == 1)
            {
                [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_curve_down"] forState:UIControlStateNormal];
            }
            else if (_intShapeMark == 2)
            {
                [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_rectangle_down"] forState:UIControlStateNormal];
            }
            else if (_intShapeMark == 3)
            {
                [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_ellipse_down"] forState:UIControlStateNormal];
            }
        
        
//        }
    }
    /// 画图
    else if ([string isEqualToString:@"2002"])
    {
        
        _viewPad.txtmode = 0;
        
        [_viewBottomToolbar.btnArrow setImage:[UIImage imageNamed:@"btn_arrow"] forState:UIControlStateNormal];
        [_viewBottomToolbar.btnArrow setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//        if (_isDrawingMode)
//        {
            [self movingView:_viewShape];
        
        _isTextButtonClicked = NO;
        
        [_viewBottomToolbar.btnAddText setImage:[UIImage imageNamed:@"btn_text"] forState:UIControlStateNormal];
        [_viewBottomToolbar.btnAddText setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        if (_kPreviousShape == 0)
        {
            _kSelectionStatus = 0;
            //        _viewPad.isPen = YES;
            _viewPad.drawTool = ACEDrawingToolTypeLine;
            
            [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_line2"] forState:UIControlStateNormal];
            [_viewBottomToolbar changeRedViewPosition:2];
            _intShapeMark = 0;
        }
        else if (_kPreviousShape == 1)
        {
            _kSelectionStatus = 1;
            //        _viewPad.isPen = YES;
            _viewPad.drawTool = ACEDrawingToolTypePen;
            
            [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_curve2"] forState:UIControlStateNormal];
            [_viewBottomToolbar changeRedViewPosition:2];
            _intShapeMark = 1;
        }
        else if (_kPreviousShape == 2)
        {
            _kSelectionStatus = 2;
            //        _viewPad.isPen = YES;
            _viewPad.drawTool = ACEDrawingToolTypeRectagleStroke;
            
            [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_rectangle2"] forState:UIControlStateNormal];
            [_viewBottomToolbar changeRedViewPosition:2];
            _intShapeMark = 2;
        }
        else if (_kPreviousShape == 3)
        {
            _kSelectionStatus = 3;
            //        _viewPad.isPen = YES;
            _viewPad.drawTool = ACEDrawingToolTypeEllipseStroke;
            
            [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_ellipse2"] forState:UIControlStateNormal];
            [_viewBottomToolbar changeRedViewPosition:2];
            _intShapeMark = 3;
        }
        
        [_viewBottomToolbar.btnDraw setTitleColor:kColorDefaultRed forState:UIControlStateNormal];
        
    }
    /// 编辑文本
    else if ([string isEqualToString:@"2003"])
    {
//        _kSelectionStatus = 5;
//        if (_isDrawingMode)
//        {
        _viewPad.txtmode = 1;
            if (_intShapeMark == 0)
            {
                [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_line_down"] forState:UIControlStateNormal];
            }
            else if (_intShapeMark == 1)
            {
                [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_curve_down"] forState:UIControlStateNormal];
            }
            else if (_intShapeMark == 2)
            {
                [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_rectangle_down"] forState:UIControlStateNormal];
            }
            else if (_intShapeMark == 3)
            {
                [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_ellipse_down"] forState:UIControlStateNormal];
            }
        
            [_viewBottomToolbar.btnDraw setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            [_viewBottomToolbar.btnArrow setImage:[UIImage imageNamed:@"btn_arrow"] forState:UIControlStateNormal];
            [_viewBottomToolbar.btnArrow setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            
            _isTextButtonClicked = YES;
            
            _viewPad.canDraw = NO;
            
            [self hideSubToolbar];
            
            [_viewBottomToolbar.btnAddText setImage:[UIImage imageNamed:@"btn_text2"] forState:UIControlStateNormal];
            [_viewBottomToolbar.btnAddText setTitleColor:kColorDefaultRed forState:UIControlStateNormal];
        
        
            [_viewBottomToolbar changeRedViewPosition:3];
            
//            [_viewPad addText];
            
//        }
    }
}


- (void)shapeView:(GMFShapeView *)shapeView buttonTagString:(NSString *)string
{
//    [_viewBottomToolbar.btnArrow setImage:[UIImage imageNamed:@"btn_arrow"] forState:UIControlStateNormal];
    
    _isArrowShow = NO;
    /// 线段
    if ([string isEqualToString:@"200"])
    {
        _kSelectionStatus = 0;
//        _viewPad.isPen = YES;
        _viewPad.drawTool = ACEDrawingToolTypeLine;
        
        [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_line2"] forState:UIControlStateNormal];
        [_viewBottomToolbar.btnDraw setTitle:@"直线" forState:UIControlStateNormal];
        [_viewBottomToolbar changeRedViewPosition:2];
        _intShapeMark = 0;
        
        _kPreviousShape = 0;
    }
    /// 笔
    else if ([string isEqualToString:@"201"])
    {
        _kSelectionStatus = 1;
//        _viewPad.isPen = NO;
        _viewPad.drawTool = ACEDrawingToolTypePen;
        
        [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_curve2"] forState:UIControlStateNormal];
        [_viewBottomToolbar.btnDraw setTitle:@"自由线" forState:UIControlStateNormal];
        [_viewBottomToolbar changeRedViewPosition:2];
        
        _intShapeMark = 1;
        
        _kPreviousShape = 1;
        
    }
    /// 矩形
    else if ([string isEqualToString:@"202"])
    {
        _kSelectionStatus = 2;
//        _viewPad.isPen = NO;
        _viewPad.drawTool = ACEDrawingToolTypeRectagleStroke;
        
        [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_rectangle2"] forState:UIControlStateNormal];
        [_viewBottomToolbar.btnDraw setTitle:@"矩形" forState:UIControlStateNormal];
        [_viewBottomToolbar changeRedViewPosition:2];
        
        _intShapeMark = 2;
        
        _kPreviousShape = 2;
    }
    /// 椭圆形
    else if ([string isEqualToString:@"203"])
    {
        _kSelectionStatus = 3;
//        _viewPad.isPen = NO;
        _viewPad.drawTool = ACEDrawingToolTypeEllipseStroke;
        
        [_viewBottomToolbar.btnDraw setImage:[UIImage imageNamed:@"btn_ellipse2"] forState:UIControlStateNormal];
        [_viewBottomToolbar.btnDraw setTitle:@"椭圆形" forState:UIControlStateNormal];
        [_viewBottomToolbar changeRedViewPosition:2];
        
        _intShapeMark = 3;
        
        _kPreviousShape = 3;
    }
    
    [self movingView:_viewShape];
}


- (void)shapeView:(GMFShapeView *)shapeView sliderValue:(float)value
{
    _viewPad.lineWidth = value;
}


- (void)colorView:(GMFColorView *)colorView andColor:(UIColor *)color
{
    _clrCurrentDrawingAndText = color;
    
    _viewPad.lineColor = color;
    _colorDrawingAndText = color;
    
    [self movingView:_viewColor];
    
    [_viewBottomToolbar.btnPalette changeButtonColor:color];
    
    [_viewPad changeColor:color];
//    _viewPad.textNormalColor = color;
//    _viewPad.textSelectColor = color;
    

   
    _viewPad.textNormalColor = color;
    
    if ((_viewPad.currentText != nil) && (CGColorEqualToColor(_viewPad.currentText.layer.borderColor, kColorPointBlue.CGColor)))
    {
//         [[NSNotificationCenter defaultCenter]postNotificationName:@"changeColor" object:nil];
        _viewPad.currentText.textColor = _clrCurrentDrawingAndText;
    }
    
   
    
    
}


@end








